package com.woniuxy.hrp.security;




import com.woniuxy.hrp.entity.Admin;
import com.woniuxy.hrp.entity.Doctor;
import com.woniuxy.hrp.entity.Patient;
import com.woniuxy.hrp.entity.Perm;
import com.woniuxy.hrp.exception.ValidateException;
import com.woniuxy.hrp.mapper.AdminMapper;
import com.woniuxy.hrp.mapper.DoctorMapper;
import com.woniuxy.hrp.mapper.PatientMapper;
import com.woniuxy.hrp.mapper.PermMapper;

import com.woniuxy.hrp.unityhandler.ResponseEntity;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.ArrayList;
import java.util.List;

public class JwtVerifyProvider implements AuthenticationProvider {
    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    private PermMapper permMapper;
    @Autowired
    private PatientMapper patientMapper;
    @Autowired
    private DoctorMapper doctorMapper;
    @Autowired
    private AdminMapper adminMapper;


    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException  {
        JwtToken jwtToken = (JwtToken) authentication;
        String jwt = (String) jwtToken.getPrincipal();//前端无jwt，此处会为"null"
        if (jwt == "null") {//对不上一个是“null”，一个是null
            throw new ValidateException("令牌无效1");
        }

        List<SimpleGrantedAuthority> authorities = new ArrayList<>();
        //jwt认证
        if (jwtUtil.verifyJWT(jwt)) {
            String account =  jwtUtil.getInfo(jwt, "account").toString();
            List<Perm> perms =null;
            Patient patient1 = patientMapper.getByAccount(account);
            if(patient1!=null){

            }
            Doctor doctor = doctorMapper.getByAccount(account);
            if(doctor!=null){
                Doctor doctor1 = doctorMapper.selectByDoctorId(doctor.getId());
                perms = doctor1.getPerms();
                for(Perm perm:perms){
                    authorities.add(new SimpleGrantedAuthority(perm.getCode()));
                }
            }
            Admin admin = adminMapper.getByAccount(account);
            if(admin!=null){

            }
        }else {
            //该处的异常无法被处理并返回前端，也无法被统一异常处理类给处理：原因在于很明显JwtFilter的祖宗是Fliter,而我们自己定义的全局异常处理器@RestControllerAdvice
            //无法捕获filter的异常
            throw new ValidateException("令牌无效");

        }
            //产生已认证的JwtToken
            JwtToken authenticatedToken = new JwtToken(jwt, true,authorities);
            //保存起来
            SecurityContextHolder.getContext().setAuthentication(authenticatedToken);
            return authenticatedToken;
    }

    @Override
    public boolean supports(Class<?> authentication) {
        return JwtToken.class.isAssignableFrom(authentication);
    }

}
